﻿using System.Collections.Generic;
using System.IO;
using Microsoft.VSPowerToys.ResourceRefactor.Common;
using XamlLocalizer.Services;

namespace XamlLocalizerAddin.ViewModels
{
    public class ResourceFileDesc : IResourceFileDesc
    {
        public ResourceFile ExistingFile { get; set; }

        public string FileName { get; set; }

        public string Text { get; set; }

        public bool IsNew
        {
            get { return ExistingFile == null; }
        }

        public ResourceFileDesc(ResourceFile existingFile)
        {
            ExistingFile = existingFile;
            FileName = ExistingFile.FileName;
            Text = ExtractText(existingFile.FileName);
        }

        private static string ExtractText(string fileName)
        {
            return Path.GetFileName(fileName);
        }

        public ResourceFileDesc(string filePath)
        {
            FileName = filePath;
            Text = ExtractText(filePath) + " (new)";
        }

        public override string ToString()
        {
            return Text;
        }

        public IEnumerable<string> GetAllKeys()
        {
            if (IsNew) return null;
            var keys = ExistingFile.Resources.Keys;
            return keys;
        }

        public void AddResource(string key, string textValue, string comment)
        {
            ExistingFile.AddResource(key, textValue, comment);
        }

        public void SaveFile()
        {
            ExistingFile.SaveFile();
        }
    }
}
